package com.weiaibenpao.demo.chislim.utils;

/**
 * Created by ZJL on 2017/9/4.
 */

public class RunningUtils {

    public static int transformTimeStrToSeconds(String time){
        String [] array =time.split(":");
        int hour =0,min =0,second =0;
        if(array.length == 3){
             hour = parseStr(array[0]);
             min = parseStr(array[1]);
             second = parseStr(array[2]);
        }else if(array.length ==2){
            min = parseStr(array[0]);
            second = parseStr(array[1]);
        }
        return 3600*hour+min*60+second;
    }

    public static int parseStr(String time){
        if(time.charAt(0)=='0'&& time.length()>1){
            return Integer.parseInt(String.valueOf(time.charAt(1)));
        }else {
            return Integer.parseInt(time);
        }
    }

    public static String transformPaceStrToSeconds(String paceStr){
        String array[] = paceStr.split("'");
        int min = parseStr(array[0]);
        int second = parseStr(array[1]);
        return ""+(min*60+second);
    }




    /**
     * 获取hh:mm:ss格式时间
     */
    public static String getHmsTime(int time) {
        String timeStr;
        int hour;
        int minute;
        int second;
        if (time <= 0)
            return "00:00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    private static String unitFormat(int i) {
        String retStr;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }


    /**
     * 获取mm:ss格式时间
     */
    public static String getMsTime(int time) {
        String timeStr;
        int hour;
        int minute;
        int second;
        if (time <= 0)
            return "00:00";
        else {
//            minute = time / 60;
//            if (minute < 60) {
//                second = time % 60;
//                timeStr = unitFormat(minute) + ":" + unitFormat(second);
//            } else {
//                hour = minute / 60;
//                if (hour > 99) return "99:59";
//                minute = minute % 60;
//                second = time - hour * 3600 - minute * 60;
//                timeStr = unitFormat(minute) + ":" + unitFormat(second);
//            }
            //上述被注释的代码有bug，例如3600秒传进来返回的是00:00
            if(time >= (99*60+59)){
                return "99:59";
            }
            minute = time /60;
            second = time %60;
            timeStr = unitFormat(minute) + ":" + unitFormat(second);
        }
        return timeStr;
    }

    /**
     * 获取语音播报的字符串
     */
    public static String getReportTimeStr(int time) {
        String timeStr;
        int hour;
        int minute;
        int second;
        if (time <= 0)
            return "0小时0分0秒";
        else {
            hour =  time/3600;
            minute =(time-hour*3600 )/60;
            second = time - hour*3600 - minute*60;//也可以写为time %60;
            if(hour>0){
                timeStr = hour+"小时"+minute + "分" + unitFormat(second)+"秒";
            }else {
                timeStr = minute + "分" + unitFormat(second)+"秒";
            }
        }
        return timeStr;
    }

}
